package com.magic.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.magic.item.mapper.BrandMapper;
import com.magic.item.service.BrandService;
import com.magic.common.pojo.PageResult;
import com.magic.item.pojo.Brand;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 品牌
 *
 * @author lan.jianyuan
 * @create 2021-01-06-18:26
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandMapper brandMapper;
    
    /**
     *根据查询条件分页并排序查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<Brand> queryBrandByPageAndSort(Integer page, Integer rows, String sortBy, boolean desc, String key) {
        //初始化example
        Example example = new Example(Brand.class);
        //根据name模糊查询，或者根据首字母查询
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().andLike("name", "%" + key + "%")
                    .orEqualTo("letter", key);
        }
        //添加分页条件
        PageHelper.startPage(page, rows);
        //添加排序条件
        if (StringUtils.isNotBlank(sortBy)) {
            // 排序
            String orderByClause = sortBy + " " + (desc ? "DESC" : "ASC");
            example.setOrderByClause(orderByClause);
        }
        List<Brand> brands = this.brandMapper.selectByExample(example);
        //包装成pageInfo
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        //包装成分更结果集返回
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        
    }
    /**
     * 新增品牌
     * @param brand
     * @param cids
     * @return
     */
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
        // 新增品牌信息
        this.brandMapper.insertSelective(brand);
        // 新增品牌和分类中间表
        for (Long cid : cids) {
            this.brandMapper.insertCategoryBrand(cid, brand.getId());
        }
    }
    /**
     * 根据品牌信息查询商品分类
     * @param bid
     * @return
     */
    @Override
    public List<Brand> selectUpdateBrand(Long bid) {
        return this.brandMapper.selectUpdateBrand(bid);
    }
    
    /**
     * 查找分类id
     * @param bid
     * @return
     */
    @Override
    public List<Long> selectCategoryId(Long bid) {
        return this.brandMapper.selectCategoryId(bid);
    }
    
    /**
     * 修改品牌
     * @param brand
     * @param cids
     */
    @Override
    @Transactional
    public void updateBrand(Brand brand, List<Long> cids) {
        this.brandMapper.updateByPrimaryKeySelective(brand);
        // 删除中间表的数据
        this.brandMapper.deleteCategoryId(brand.getId());
        // 往表重新填数据
        for (Long cid : cids) {
            this.brandMapper.insertCategoryBrand(cid, brand.getId());
        }
    }
    
    /**
     * 删除品牌
     * @param bid
     */
    @Override
    @Transactional
    public void deleteBrand(Long bid) {
        this.brandMapper.deleteBrand(bid);
        this.brandMapper.deleteCategoryId(bid);
    }
    /**
     * 根据分类id查询品牌列表
     * @param cid
     * @return
     */
    public List<Brand> queryBrandsByCid(Long cid) {
        return this.brandMapper.selectBrandByCid(cid);
    }
    
    /**
     * 根据分类id查询品牌名称
     * @param id
     * @return
     */
    public Brand queryBrandById(Long id) {
        return this.brandMapper.selectByPrimaryKey(id);
    }
}
